/*
 * @文件描述:UI相关处理
 * @版本:
 * @作者:周晨阳
 * @Date:2021-09-19 10:45:05
 */

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using myConection;
using System;

public class View : MonoBehaviour
{
    //连接按钮
    private Button connectBut;
    //断联按钮
    private Button disconnectBut;

    private GameObject conObj;
    private Text conStateText;

    //连接类的对象
    connection conection;

    void Awake()
    {
        conStateText = GameObject.Find("conState").GetComponent<Text>();

    }
    // Start is called before the first frame update
    /**
     * @brief :关联一些控件，获得相应的对象实体
     * @param {*}
     * @return {*}
     */
    void Start()
    {
        conObj = GameObject.Find("GameObject");
        conection = (myConection.connection)conObj.GetComponent(typeof(myConection.connection));

        connectBut = GameObject.Find("connect").GetComponent<Button>();
        disconnectBut = GameObject.Find("disconnect").GetComponent<Button>();

        connectBut.onClick.AddListener(OnConnectClickFunction);
        disconnectBut.onClick.AddListener(onDisconnectClickFunction);

    }

    // // Update is called once per frame
    // void Update()
    // {

    // }

    private void OnConnectClickFunction()
    {
        //Debug.Log("connect clicked");
        conection.connectToDevice();
        
    }
    private void onDisconnectClickFunction()
    {
        //Debug.Log("disconnect clicked");
        conection.disConnect();

    }
    /// <summary>
    /// 向界面的输出窗口显示信息,注意,这里使用了loom,因为涉及到子线程和UI线程的交互
    /// </summary>
    /// <param name="info"></param>
    public void showInfo(String info)
    {
        Loom.QueueOnMainThread((param) =>
        {
            conStateText.text = info;
        }, null);

    }
}
